import os
import csv

def clean_and_save_csv_to_txt(path_csv, path_txt):
    # 读取CSV文件
    with open(path_csv, mode='r', newline='') as csvfile:
        reader = csv.reader(csvfile)
        data = list(reader)

    # 清理数据
    cleaned_data = []
    for row in data:
        cleaned_row = []
        for item in row:
            item = item.replace('1.000000000000000000e+00', '1')
            item = item.replace('0.000000000000000000e+00', '0')
            item = item.replace('3.141592653589793', '1')
            item = item.replace('1.0', '1')
            item = item.replace('0.0', '0')
            item = item.replace(',', '')
            # item = item.replace(' ', '')
            cleaned_row.append(item)
        cleaned_data.append(cleaned_row)

    # 将清理后的数据保存到TXT文件
    with open(path_txt, mode='w') as txtfile:
        for row in cleaned_data:
            txtfile.write(''.join(row) + '\n')



def process_all_csv_files_in_directory(path_dir):
    if not os.path.exists(path_dir):
        print(f"error, not exist: {path_dir}")
        return
    for filename in os.listdir(path_dir):
        if filename.endswith('.csv'):
            print(f"Processing file: {filename}")
            path_csv = os.path.join(path_dir, filename)
            path_txt = os.path.join(path_dir, filename.replace("csv", "txt"))
            print(f"path_csv:{path_csv}, path_txt:{path_txt}")
            clean_and_save_csv_to_txt(path_csv, path_txt)



def test():
    # 示例路径
    path_csv = '../files/tools/02.csv+txt/17_beam4_AGA_(((30,0),(30,90),(30,180),(30,270)))-30.csv'
    # path_txt = '../files/tools/02.csv+txt/07_beam1_AGA_(50,0).txt'

    path_list = [
        {"csv": "../files/tools/02.csv+txt/10_beam2_AGA_(((30,0),(30,90))).csv",
         "txt": "../files/tools/02.csv+txt/10_beam2_AGA_(((30,0),(30,90))).txt"},
        {"csv": "../files/tools/02.csv+txt/11_beam2_AGA_(((30,0),(30,120))).csv",
         "txt": "../files/tools/02.csv+txt/11_beam2_AGA_(((30,0),(30,120))).txt"},
        {"csv": "../files/tools/02.csv+txt/12_beam2_AGA_(((30,0),(30,180))).csv",
         "txt": "../files/tools/02.csv+txt/12_beam2_AGA_(((30,0),(30,180))).txt"},
        {"csv": "../files/tools/02.csv+txt/13_beam4_AGA_(((20,0),(20,60),(20,120),(20,180)))-30.csv",
         "txt": "../files/tools/02.csv+txt/13_beam4_AGA_(((20,0),(20,60),(20,120),(20,180)))-30.txt"},
        {"csv": "../files/tools/02.csv+txt/14_beam4_AGA_(((20,0),(20,90),(20,180),(20,270)))-30.csv",
         "txt": "../files/tools/02.csv+txt/14_beam4_AGA_(((20,0),(20,90),(20,180),(20,270)))-30.txt"},
        {"csv": "../files/tools/02.csv+txt/15_beam4_AGA_(((20,0),(20,90),(20,180),(20,270)))-100.csv",
         "txt": "../files/tools/02.csv+txt/15_beam4_AGA_(((20,0),(20,90),(20,180),(20,270)))-100.txt"},
        {"csv": "../files/tools/02.csv+txt/16_beam4_AGA_(((30,0),(30,60),(30,120),(30,180)))-30.csv",
         "txt": "../files/tools/02.csv+txt/16_beam4_AGA_(((30,0),(30,60),(30,120),(30,180)))-30.txt"},
        {"csv": "../files/tools/02.csv+txt/17_beam4_AGA_(((30,0),(30,90),(30,180),(30,270)))-30.csv",
         "txt": "../files/tools/02.csv+txt/17_beam4_AGA_(((30,0),(30,90),(30,180),(30,270)))-30.txt"},
    ]

    for path in path_list:
        path_csv = path["csv"]
        path_txt = path["txt"]
        # 调用函数进行处理
        clean_and_save_csv_to_txt(path_csv, path_txt)



if __name__=="__main__":
    process_all_csv_files_in_directory("../files/tools/02.csv+txt/")